package com.atguigu.exer;

public class CommunicationTest {
    public static void main(String[] args) {
        Number number=new Number();
        Thread thread1=new Thread(number);
        Thread thread2=new Thread(number);
        thread1.setName("线程1");
        thread2.setName("线程2");
        thread1.start();
        thread2.start();
    }
}
class Number implements Runnable{
    private int number=1;
    public void run(){
        while(true){
            synchronized (this) {
                notify();
                if (number <= 100) {
                    System.out.println(Thread.currentThread().getName() + ":" + number);
                    number++;
                    try {
                        //使得调用如下wait()方法的线程进入阻塞状态
                        wait();
                    }catch(InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    break;
                }
            }
        }
    }
}
